﻿using System;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Load: MonoBehaviour
{
    private string  BundleURL = "file:///C:/cube.assetbundle";
    private string  SceneURL = "file:///C:/scene1.unity3d";
    private String  PlaneURL = "file:///C:/plane2.assetbundle";
    private String  TankURL = "file:///C:/tank.assetbundle";
    private String  TotalURL = "file:///C:/total.assetbundle";
    private WWW     sceneLoader = null;
    public Text     m_Hint;

    void Start()
    {
        Debug.Log(BundleURL);
        StartCoroutine(DownloadAssetAndScene());
        m_Hint.text = "loading";

        Debug.Log("path : " + Application.persistentDataPath);
    }

    IEnumerator DownloadAssetAndScene()
    {
        AssetBundle bundle1;
        //下载assetbundle，加载Cube
        //using (WWW asset = new WWW("file:///C:/red.assetbundle"))
        using (WWW asset = new WWW("http://192.168.1.128:9888/images/red.assetbundle"))
        {
            yield return asset;
            bundle1 = asset.assetBundle;
            //bundle1.Unload(false);
            m_Hint.text = "loading sphere";
            FileUtil.WriteFileByte(Application.persistentDataPath + "//" + "red.bundle", asset.bytes, asset.bytes.Length);
            yield return new WaitForSeconds(2);
        }
        using (WWW asset = new WWW("file:///C:/Sphere.assetbundle"))
        {
            yield return asset;
            AssetBundle bundle = asset.assetBundle;
            Instantiate(bundle.Load("Sphere"));
            bundle.Unload(false);
            m_Hint.text = "loading cube";
            yield return new WaitForSeconds(2);
        }

        using (WWW asset = new WWW("file:///C:/Cube.assetbundle"))
        {
            yield return asset;
            AssetBundle bundle = asset.assetBundle;
            Instantiate(bundle.Load("Cube"));
            bundle.Unload(false);
            m_Hint.text = "loading tank";
            yield return new WaitForSeconds(2);
        }
        bundle1.Unload(false);

        using (WWW asset = new WWW("file:///C:/tank.assetbundle"))
        {
            yield return asset;
            AssetBundle bundle = asset.assetBundle;
            Instantiate(bundle.Load("Tank"));
            bundle.Unload(false);
            m_Hint.text = "loading plane";
            yield return new WaitForSeconds(2);
        }

        using (WWW asset = new WWW("file:///C:/plane.assetbundle"))
        {
            yield return asset;
            AssetBundle bundle = asset.assetBundle;
            Instantiate(bundle.Load("Plane"));
            bundle.Unload(false);
            m_Hint.text = "loading done";
            yield return new WaitForSeconds(2);
        }
    }

    void Update()
    {
        if (sceneLoader != null)
        {
            Debug.Log("progress : " + sceneLoader.progress);
            m_Hint.text = "" + sceneLoader.progress;
        }
    }

}